package com.lrc.dentalbackend.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.parser.Feature;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("开启扩展信息转换器，使用Fastjson");

        // 创建FastJsonHttpMessageConverter的实例
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();

        // 创建FastJsonConfig的实例并配置serializerFeatures和features
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue, // 输出空置字段
                SerializerFeature.PrettyFormat       // 美化输出
                // 可以添加其他Feature
        );
        fastJsonConfig.setFeatures(
                Feature.AutoCloseSource,
                Feature.InternFieldNames
                // 可以添加其他Feature
        );

        // 设置FastJsonConfig到FastJsonHttpMessageConverter
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);

        // 将FastJsonHttpMessageConverter添加到converters列表的第一个位置
        converters.set(0, fastJsonConverter);
    }
}
